package FangXing.day10.generics;

import object.toString.A;

import java.util.ArrayList;

public class GenericsDemo4 {
    /*
    泛型通配符：
        ?:任意类型
        ? extends E:可以传入的是E或者E的子类
        ？ super E :可以传入的是E或者他的父类
     */
    public static void main(String[] args) {
        ArrayList<Coder> list1 = new ArrayList<>();
        list1.add(new Coder());

        ArrayList<Manager> list2 = new ArrayList<>();
        list2.add(new Manager());

        ArrayList<String> list3 = new ArrayList<>();
        list3.add("abc");
//        method(list3);//类型限制报错
        ArrayList<Object> list4 = new ArrayList<>();
        method2(list4);

        method(list1);
        method(list2);

    }

    public static void method(ArrayList<? extends Employee> list){//泛型限制只能传入Employee或者她的子类
        for (Object o : list) {
            Employee e = (Employee)o;//转为他的父类则俩个子类都能接
            e.work();
        }
    }
    public static void method2(ArrayList<? super Employee> list){//泛型限制只能传入Employee或者她的子类
        for (Object o : list) {
            Employee e = (Employee)o;//转为他的父类则俩个子类都能接
            e.work();
        }
    }
}

abstract class Employee{
    private String name;
    private double salary;

    public abstract void work();

    public Employee() {
    }

    public Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", salary=" + salary +
                '}';
    }
}

class Coder extends Employee{

    @Override
    public void work() {
        System.out.println("程序员写代码");
    }
}

class Manager extends Employee{

    @Override
    public void work() {
        System.out.println("项目经理分配任务");
    }

}
